package com.zx.cloud.order.service.impl;

import com.zx.cloud.common.result.R;
import com.zx.cloud.order.api.feign.RemoteStockService;
import com.zx.cloud.order.model.OrderTbl;
import com.zx.cloud.order.mapper.OrderTblMapper;
import com.zx.cloud.order.service.OrderTblService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhaoxuan
 * @since 2021-12-22
 */
@Service
@Slf4j
public class OrderTblServiceImpl extends ServiceImpl<OrderTblMapper, OrderTbl> implements OrderTblService {

    @Autowired
    private RemoteStockService remoteStockService;

    @Autowired
    private  OrderImpl order;

    @Autowired
    private OrderTblMapper orderTblMapper;
    /**
     * 下单：创建订单、减库存，涉及到两个服务
     *
     * @param userId
     * @param commodityCode
     * @param count
     */
    @GlobalTransactional
    @Override
    public void placeOrder(String userId, String commodityCode, Integer count) {
        log.info("当前 XID: {}", RootContext.getXID());
        /*Map<String, String> headers = new HashMap<>();
        headers.put(RootContext.KEY_XID, RootContext.getXID());*/
        remoteStockService.deduct(commodityCode, count);

        order.createOrder(userId, commodityCode, count);
        throw new RuntimeException("下单失败");
    }


}
